export function getParams(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return false;
}

export function getToday() {
	let date = new Date();
	let str =
		date.getFullYear() +
		"-" +
		(date.getMonth() + 1 < 10 ?
			"0" + (date.getMonth() + 1) :
			date.getMonth() + 1) +
		"-" +
		(date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
	return str;
}

export function getToken() {
	return uni.getStorageSync("token");
}

export function getUserId() {
	return uni.getStorageSync("userId");
}

export function getNowDate() {
	var date = new Date();
	var sign2 = ":";
	var year = date.getFullYear(); // 年
	var month = date.getMonth() + 1; // 月
	var day = date.getDate(); // 日
	var hour = date.getHours(); // 时
	var minutes = date.getMinutes(); // 分
	var seconds = date.getSeconds(); //秒
	var weekArr = [
		"星期一",
		"星期二",
		"星期三",
		"星期四",
		"星期五",
		"星期六",
		"星期天",
	];
	var week = weekArr[date.getDay()];
	// 给一位数的数据前面加 “0”
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (day >= 0 && day <= 9) {
		day = "0" + day;
	}
	if (hour >= 0 && hour <= 9) {
		hour = "0" + hour;
	}
	if (minutes >= 0 && minutes <= 9) {
		minutes = "0" + minutes;
	}
	if (seconds >= 0 && seconds <= 9) {
		seconds = "0" + seconds;
	}
	return (
		year +
		"-" +
		month +
		"-" +
		day +
		" " +
		hour +
		sign2 +
		minutes +
		sign2 +
		seconds
	);
}

export function formatDate(time) {
	var date = new Date(time);
	var year = date.getFullYear(),
		month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(),
		hour = date.getHours(),
		min = date.getMinutes(),
		sec = date.getSeconds();
	var newTime =
		year +
		"-" +
		(month < 10 ? "0" + month : month) +
		"-" +
		(day < 10 ? "0" + day : day) +
		" " +
		(hour < 10 ? "0" + hour : hour) +
		":" +
		(min < 10 ? "0" + min : min) +
		":" +
		(sec < 10 ? "0" + sec : sec);

	return newTime;
}

export function getDayInfo(diff) {
	let date = new Date().getTime();
	if (diff < 0) {
		date += diff * 24 * 60 * 60 * 1000;
	}
	let date1 = new Date(date);
	let year = date1.getFullYear(),
		month = date1.getMonth() + 1,
		day = date1.getDate();
	let newTime =
		year +
		"-" +
		(month < 10 ? "0" + month : month) +
		"-" +
		(day < 10 ? "0" + day : day);
	return newTime;
}

// 把数组转化成树形结构
export const transformArrtoTree = function(arr) {
	if (!Array.isArray(arr)) return;
	let result = [];
	for (let i = 0; i < arr.length; i++) {
		// if (arr[i].pid == "0") {
		arr[i].children = arr.filter((item) => item.pid == arr[i].id);
		result.push(arr[i]);
		// }
	}
	return result;
};

// 将数组转为树，要求必须是可以组成树的数据
export const arrayToTree = (menus) => {
	const currentMenus = JSON.parse(JSON.stringify(menus));
	const menuTree = [];
	const menuMap = {
		0: "true",
	};
	let menuPidRoutes = [];
	let currentStep = 0;
	let currMenu;
	while (currentMenus.length !== 0) {
		currMenu = currentMenus[currentStep];
		// 判断当前pid是否是0或存在
		if (menuMap[currMenu.pid]) {
			menuPidRoutes = [];
			let currentMenuId = currMenu.pid;
			// 获取所有父类菜单的id
			while (currentMenuId != "0") {
				menuPidRoutes.unshift(currentMenuId);
				currentMenuId = menuMap[currentMenuId];
			}
			const tempMenu = {
				title: currMenu.meta.title, // --> db: name
				path: currMenu.path, // --> db: link
				id: currMenu.id,
				imgSrc: currMenu.meta.icon || "index-icon3.png", // --> db: icon
				seq: currMenu.seq,
				type: currMenu.type, // 0-目录-二级菜单 1-菜单
				openType: currMenu.openType, // 1-内链 5-外链 6-报表
			};
			// 判断是否是根菜单
			if (menuPidRoutes.length === 0) {
				tempMenu.root = true;
				menuTree.push(tempMenu);
			} else {
				// 处理子菜单
				let currentMenuOptions = menuTree;
				const len = menuPidRoutes.length;
				menuPidRoutes.forEach((id, curr) => {
					const index = currentMenuOptions.findIndex((menu) => menu.id === id);
					if (!currentMenuOptions[index].children) {
						currentMenuOptions[index].children = [];
					}
					if (len - 1 > curr) {
						currentMenuOptions = currentMenuOptions[index].children || [];
					} else {
						currentMenuOptions[index].children?.push(tempMenu);
					}
				});
			}
			menuMap[currMenu.id] = currMenu.pid;
			currentMenus.splice(currentStep, 1);
		} else {
			currentStep++;
		}
		if (currentStep >= currentMenus.length) {
			currentStep = 0;
		}
	}

	return menuTree;
};

export function transformData(treeData) {
	if (!Array.isArray(treeData)) return;
	let res = treeData.map((item) => {
		return {
			label: item.equtypename,
			id: item.idK,
			choose: false,
			openTag: false,
			children: transformData(item.children),
			leaf: item.children && item.children.length > 0 ? false : true
		};
	});
	return res;
}